package com.nursinghome.emp.controller;

import com.nursinghome.bean.MedicalStaff;
import com.nursinghome.bean.RespBean;
import com.nursinghome.emp.service.EmpMedicalStaffService;
import com.nursinghome.group.MedicalUpdateGroup;
import com.nursinghome.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/emp/medical")
public class EmpMedicalController {
    @Autowired
    private EmpMedicalStaffService empMedicalStaffService;
    @GetMapping()
    public RespBean empInfo(@RequestHeader(value = "token", required = false) String token) {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Integer jwtMid = (Integer) map.get("mid");
        if (jwtMid == null) {
            return RespBean.error("令牌信息不正确");
        }
        MedicalStaff medicalStaff = empMedicalStaffService.selectByMid(jwtMid);
        return RespBean.ok("你好员工！",medicalStaff);
    }
    //修改护工信息
    @PutMapping
    public RespBean updateMedical(@RequestBody @Validated(MedicalUpdateGroup.class) MedicalStaff medicalStaff,
                                  @RequestHeader(value = "token", required = false) String token){
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Integer jwtMid = (Integer) map.get("mid");
        if (jwtMid == null) {
            return RespBean.error("令牌信息不正确");
        }
        medicalStaff.setMid(jwtMid);

        if (!empMedicalStaffService.updateMedical(medicalStaff)) {
            return RespBean.error("修改失败");
        }
        return RespBean.ok("修改成功");
    }
}
